package com.by.service.impl.impl;

import com.by.bean.ForwarderInquirySheet;
import com.by.bean.ForwarderProduct;
import com.by.bean.ForwarderQuoteList;
import com.by.bean.util.SheetType;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.mapper.ForwarderInquirySheetMapper;
import com.by.mapper.ForwarderProductMapper;
import com.by.mapper.ForwarderQuoteListMapper;
import com.by.mapper.UserMapper;
import com.by.query.ForwarderQuery;
import com.by.query.FreightInquiryQuery;
import com.by.service.impl.ForwarderQuoteListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class ForwarderQuoteListServiceImpl extends BaseServiceImpl<ForwarderQuoteList, ForwarderQuoteListMapper>implements ForwarderQuoteListService {
    @Autowired
    private ForwarderProductMapper forwarderProductMapper;
    @Autowired
    private ForwarderInquirySheetMapper forwarderInquirySheetMapper;

    @Override
    public int save(ForwarderQuoteList entity) {
        // 设置产品信息中的询价编码
        List<ForwarderProduct> products = entity.getProducts();
        if (products != null && !products.isEmpty()) {
            List<ForwarderProduct> newProducts = new ArrayList<>();
            for (ForwarderProduct product : products) {
                // 设置询价编码
                product.setQuote_coding(entity.getQuote_coding());
                // 使用有参构造创建新的ForwarderProduct对象
                ForwarderProduct newProduct = new ForwarderProduct(
                        entity.getQuote_coding(),
                        product.getProduct_number(),
                        product.getProduct_name(),
                        product.getPieces(),
                        product.getGross_weight(),
                        product.getVolume(),
                        product.getSupport_number(),
                        entity.getCustomer_id()
                );
                newProducts.add(newProduct);
            }
            // 批量保存产品信息
            int rs = forwarderProductMapper.saveBatch(newProducts);
            Assert.isZero(rs, "保存产品信息失败");
        }

        // 生成订单编码，订单编码 = 客户id + 四位随机数
        Random random = new Random();
        int fourDigitNumber = random.nextInt(9000) + 1000;
        String order_sn = entity.getCustomer_id() + "" + fourDigitNumber;

        ForwarderInquirySheet sheet = new ForwarderInquirySheet(null, entity.getQuote_coding(), order_sn, SheetType.DBJ, "先占着这个字段，后面再填", entity.getQuote_data());
        int rse = forwarderInquirySheetMapper.save(sheet);
        Assert.isZero(rse, "保存询价单失败");

        return super.save(entity);
    }
   /* @Override
    public PageBean query(FreightInquiryQuery query) {
        //分页查询
        PageHelper.startPage(query.getPage(),query.getPageSize());//后面紧跟查询
        List<Map> list = baseMapper.query(query);
        PageInfo info = new PageInfo(list);
        return new PageBean(query.getPage(),query.getPageSize(),info.getTotal(),list);
    }*/


}





